<?php
namespace Tbf\Bundle\PackageBundle\Tests;

use Tbf\Component\FileSystem\FileSystemFunction;
use Tbf\Bundle\PackageBundle\Annotation\Annotation;
use Tbf\Bundle\PackageBundle\ClassGenerator\EntityGenerator;
use Tbf\Bundle\PackageBundle\ClassGenerator\EntityDefine;
use Tbf\Bundle\PackageBundle\ClassGenerator\PhpProperty;

class EntityGeneratorTest extends PackageBundleTestCase
{
    protected $test_dir;
    public function setUp()
    {
        $this->markTestIncomplete('此功能因为GC组件不完善而无法使用...');
        $this->test_dir = __DIR__.'/cache';
    }
    public function testGenerate()
    {
        $pc = $this->getMockEntityDefine();
        $eg = new EntityGenerator();
        $except = $this->getMockEntityContent();
       $this->assertEquals($except,$eg->generate($pc));
    }

    public function testGenerateToFile()
    {
        $target_path = $this->test_dir.'/Abd.php';
        $pc = $this->getMockEntityDefine();
        $pc->setFilePath($target_path);
        $eg = new EntityGenerator();
        $eg->generateToFile($pc);
        $expect = '<?php'."\n".$this->getMockEntityContent();
        $this->assertEquals($expect,file_get_contents($target_path));
    }
    public function getMockEntityDefine()
    {
        $pc = EntityDefine::create('\AbcVendor\Entity\Abd')
            ->setParentClassName('AbcVendor\Abc')
            ->addAnnotation(new Annotation('\Doctrine\ORM\Mapping\Table',array('name'=>'abd')))
            ->addAnnotation(new Annotation('\Gedmo\Mapping\Annotation\Tree',array('type'=>'closure')))
            ->addAnnotation(new Annotation('\Gedmo\Mapping\Annotation\TreeClosure',array('class'=>'AbcVendor\Entity\AbdClosure')))
            ->addAnnotation(new Annotation('\Doctrine\ORM\Mapping\Table',array('repositoryClass'=>'Gedmo\Tree\Entity\Repository\ClosureTreeRepository')))
            ->setProperty(PhpProperty::create('id')
                ->setVisibility('protected')
                ->addAnnotation(new Annotation('\Doctrine\ORM\Mapping\Column',array('name'=>'id','type'=>'integer')))
                ->addAnnotation(new Annotation('\Doctrine\ORM\Mapping\Id'))
                ->addAnnotation(new Annotation('\Doctrine\ORM\Mapping\GeneratedValue'))
            )
            ->setProperty(
                PhpProperty::create('name')
                    ->setVisibility('protected')
                    ->addAnnotation(new Annotation('\Doctrine\ORM\Mapping\Column'))
            );

        return $pc;
    }
    public function getMockEntityContent()
    {
        return <<<'PHP'
namespace Tbf\Bundle\PackageBundle\Tests;

/**
 * @Doctrine\ORM\Mapping\Table(name="abd")
 * @Gedmo\Mapping\Annotation\Tree(type="closure")
 * @Gedmo\Mapping\Annotation\TreeClosure(class="AbcVendor\Entity\AbdClosure")
 * @Doctrine\ORM\Mapping\Table(repositoryClass="Gedmo\Tree\Entity\Repository\ClosureTreeRepository")
 */
class Abd extends \AbcVendor\Abc
{
    /**
     * @Doctrine\ORM\Mapping\Column()
     */
    protected $name;
    /**
     * @Doctrine\ORM\Mapping\Column(name="id", type="integer")
     * @Doctrine\ORM\Mapping\Id()
     * @Doctrine\ORM\Mapping\GeneratedValue()
     */
    protected $id;
}
PHP;
    }
    public function tearDown()
    {
        FileSystemFunction::delete($this->test_dir);
    }
}
